package com.jsnkj.basic.activity

import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.view.View
import com.jsnkj.basic.R
import com.jsnkj.basic.config.SpConfig
import com.jsnkj.basic.databinding.ActivitySettingBinding
import com.jsnkj.basic.mvvm.BaseMVVMActivity
import com.jsnkj.basic.mvvm.HttpViewModel
import com.jsnkj.basic.util.SharePreferencesUtil
import kotlin.system.exitProcess


/**
 * 设置界面
 */
class SettingActivity : BaseMVVMActivity<ActivitySettingBinding, HttpViewModel>(),
    View.OnClickListener {
    override val layoutId: Int
        get() = R.layout.activity_setting

    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this

        mVDB.topTitle.txtTopTitle.text = "我的设置"
        mVDB.topTitle.imageReturn.setOnClickListener {
            onBackPressed()
        }
        mVDB.topTitle.layoutTopTitle.setBackgroundColor(Color.parseColor("#FFFFFF"))

        SharePreferencesUtil.getInstance().getUserBean()?.let { userEntity ->
            userEntity.user?.let { user ->
                mVDB.tvUserName.text = user.showName()
                mVDB.tvUserId.text = user.uuid
                mVDB.tvMasterId.text = user.parent_uuid
            }
        }

        try {
            mVDB.tvVersion.text = "V${packageManager.getPackageInfo(packageName, 0).versionName}"
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
            mVDB.tvVersion.text = "V0.0.1"
        }
    }


    override fun onClick(view: View) {
        when (view.id) {
            R.id.lin_layout_logout -> {
                SharePreferencesUtil.getInstance().putUserBean(null)
                SharePreferencesUtil.getInstance().putBoolean(SpConfig.IS_LOGIN, false)

                Handler().postDelayed({
                    // 进行内存优化，销毁掉所有的界面
                    com.jsnkj.basic.util.ActivityStackManager.getInstance().finishAllActivities()
                    exitProcess(0)
                }, 300)
            }
        }
    }
}